package cc.vv.baselibrary.view.util;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Calendar;
import java.util.Date;

import cc.vv.baselibrary.R;
import cc.vv.baselibrary.view.pickerview.TimePickerView;
import cc.vv.baselibrary.view.pickerview.listener.CustomListener;


/**
 * 出生日期选择dialog
 *
 * @author chaohaipeng  on 2017/12/12.
 */

public class DateChoseDialog {
    /**
     * 单例对象
     */
    private static DateChoseDialog sInstance;
    /**
     * 时间选择控件
     */
    private TimePickerView pvCustomTime;
    /**
     * 日期选择回调
     */
    private BrithDateChoseInter brithDateChoseInter;
    /**
     * 日期
     */
    private Date mDate;
    /**
     * 显示格式  月,日,时,分
     */
    public static final int DATE_TYPE_1 = 1;
    /**
     * 显示格式  年,月,日
     */
    public static final int DATE_TYPE_2 = 2;

    /**
     * 显示格式  年,月,日--2050.01.01
     */
    public static final int DATE_TYPE_3 = 3;

    /**
     * 实例化对象
     *
     * @return
     */
    public static DateChoseDialog getInstance() {
        if (sInstance == null) {
            sInstance = new DateChoseDialog();
        }
        return sInstance;
    }

    /**
     * 日期选择回调
     *
     * @param brithDateChoseInter
     */
    public void setBrithDatenterface(BrithDateChoseInter brithDateChoseInter) {
        this.brithDateChoseInter = brithDateChoseInter;

    }

    public void showBirthDateDialog() {
        if (pvCustomTime != null) {
            pvCustomTime.show();
        }
    }

    /**
     * 初始化日期选则  默认选中当前时间
     *
     * @param context  当前activity对象
     * @param dateType 需要显示那些类别   暂时提供两种样式
     */
    public void initBirthDatePicker(Context context, int dateType) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int min = calendar.get(Calendar.MINUTE);
        initBirthDatePicker(context, year, month, day, hour, min, dateType);
    }


    /**
     * 初始化日期选则//defualtMonth为0表示是1月份
     *
     * @param context      当前activity对象
     * @param defualtYear  默认显示年
     * @param defualtMonth 默认显示月
     * @param defualtDay   默认显示日
     * @param dateType     需要显示那些类别   暂时提供两种样式
     */
    public void initBirthDatePicker(Context context,
                                    int defualtYear,
                                    int defualtMonth,
                                    int defualtDay,
                                    int dateType) {

        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int min = calendar.get(Calendar.MINUTE);
        initBirthDatePicker(context, defualtYear, defualtMonth, defualtDay, hour, min, dateType);

    }

    /**
     * 初始化日期选则//defualtMonth为0表示是1月份
     *
     * @param context      当前activity对象
     * @param defualtYear  默认显示年
     * @param defualtMonth 默认显示月   0~11
     * @param defualtDay   默认显示日   1~31
     * @param defualtHour  默认显示小时  0~23
     * @param defualtMin   默认显示分钟  0~59
     * @param dateType     需要显示那些类别   暂时提供两种样式
     */
    public void initBirthDatePicker(Context context,
                                    int defualtYear,
                                    int defualtMonth,
                                    int defualtDay,
                                    int defualtHour,
                                    int defualtMin,
                                    int dateType) {

        //设置显示格式
        boolean[] dateTypeArr = new boolean[]{true, true, true, false, false, false};
        //设置最大选择日期  默认为当前时间
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        int startYear = 1900;
        int startMonth = 0;
        int startDay = 1;

        int endYear = year;
        int endMonth = month;
        int endDay = day;

        if (dateType == DATE_TYPE_1) {
            //发送dang显示格式  月份选择全年
            dateTypeArr = new boolean[]{false, true, true, true, true, false};

            startYear = year;
            startMonth = 0;
            startDay = day;

            endYear = year;
            endMonth = 11;
            endDay = 31;
        } else if (dateType == DATE_TYPE_2) {
            //用户信息显示格式  最大选择时间为当前日期
            dateTypeArr = new boolean[]{true, true, true, false, false, false};

            startYear = 1900;
            startMonth = 0;
            startDay = 1;
            endYear = year;
            endMonth = month;
            endDay = day;
        } else if (dateType == DATE_TYPE_3) {
            //用户信息显示格式  最大选择时间为当前日期
            dateTypeArr = new boolean[]{true, true, true, false, false, false};

            startYear = 1900;
            startMonth = 0;
            startDay = 1;
            endYear = 2050;
            endMonth = 0;
            endDay = 1;
        }
        if (defualtYear > 0 && defualtDay > 0) {

            /**
             * @description
             *
             * 注意事项：
             * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
             * 具体可参考demo 里面的两个自定义layout布局。
             * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
             * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
             */
            //系统当前时间
            Calendar selectedDate = Calendar.getInstance();
            selectedDate.set(defualtYear, defualtMonth, defualtDay, defualtHour, defualtMin);
            Calendar startDate = Calendar.getInstance();
            startDate.set(startYear, startMonth, startDay);
            Calendar endDate = Calendar.getInstance();
            endDate.set(endYear, endMonth, endDay);

            //是否只显示中间选中项的label文字，false则每项item全部都带有label。
            pvCustomTime = new TimePickerView.Builder(context, new TimePickerView.OnTimeSelectListener() {
                @Override
                public void onTimeSelect(Date date, View v) {
                    mDate = date;
                }
            })
                    .setLineSpacingMultiplier(2.0f)
                    .setDate(selectedDate)
                    .setRangDate(startDate, endDate)
                    .setBackgroundId(Color.parseColor("#73000000"))
                    .setLayoutRes(R.layout.pickerview_birth_date_chose, new CustomListener() {

                        @Override
                        public void customLayout(View v) {
                            final TextView tvSubmit = v.findViewById(R.id.tv_bc_sure);
                            TextView ivCancel = v.findViewById(R.id.tv_bc_cancel);
                            RelativeLayout rl_date_choose_layout = v.findViewById(R.id.rl_date_choose_layout);
                            tvSubmit.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    pvCustomTime.dismiss();
                                    if (brithDateChoseInter != null) {
                                        pvCustomTime.returnData();
                                        brithDateChoseInter.resultData(mDate);
                                    }

                                }
                            });
                            ivCancel.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    pvCustomTime.dismiss();
                                }
                            });
                            //拦截按钮一栏空白区域点击弹框消失问题
                            rl_date_choose_layout.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    return;
                                }
                            });
                        }
                    })
                    .setType(dateTypeArr)
                    //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                    .isCenterLabel(false)
                    .setDividerColor(Color.parseColor("#C3DEFA"))
                    .build();


        }

    }

}
